<?php

use app\core\auth\Identity;
use filsh\yii2\oauth2server\Module;
use filsh\yii2\oauth2server\Request;
use filsh\yii2\oauth2server\Response;
use OAuth2\GrantType\RefreshToken;
use OAuth2\GrantType\UserCredentials;

$params = array_merge(
    require __DIR__ . '/params.php',
    require __DIR__ . '/params_local.php'
);

$db = require __DIR__ . '/test_db.php';

/**
 * Application configuration shared by all test types
 */
return [
    'id' => 'basic-tests',
    'basePath' => dirname(__DIR__),
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'language' => 'en-US',
    'modules' => [
        'oauth2' => [
            'class' => Module::class,
            'tokenParamName' => 'accessToken',
            'tokenAccessLifetime' => 3600 * 24,
            'storageMap' => [
                'user_credentials' => Identity::class,
            ],
            'components' => [
                'request' => function () {
                    return Request::createFromGlobals();
                },
                'response' => [
                    'class' => Response::class,
                ],
            ],
            'grantTypes' => [
                'user_credentials' => [
                    'class' => UserCredentials::class,
                ],
                'refresh_token' => [
                    'class' => RefreshToken::class,
                    'always_issue_new_refresh_token' => true
                ],
            ],
        ],
    ],
    'components' => [
        'db' => $db,
        'mailer' => [
            'class' => \yii\symfonymailer\Mailer::class,
            'viewPath' => '@app/mail',
            // send all mails to a file by default.
            'useFileTransport' => true,
            'messageClass' => 'yii\symfonymailer\Message'
        ],
        'assetManager' => [
            'basePath' => __DIR__ . '/../web/assets',
        ],
        'user'               => [
            'identityClass' => Identity::class,
        ],
        'request' => [
            'cookieValidationKey' => 'test',
            'enableCsrfValidation' => false,
            // but if you absolutely need it set cookie domain to localhost
            /*
            'csrfCookie' => [
                'domain' => 'localhost',
            ],
            */
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
//                ['class' => 'yii\rest\UrlRule', 'controller' => 'auth'],
//                ['class' => 'yii\rest\UrlRule', 'controller' => 'site'],

                'POST oauth2/<action:\w+>' => 'oauth2/rest/<action>',

                '' => 'site/index',
                'GET site/check-auth' => 'site/check-auth',

                'POST auth/login' => 'auth/login',
                'GET auth/user' => 'auth/user',

                'GET user/data' => 'user/data',

                'OPTIONS oauth2/<c:[\w-]+>' => '',
                'OPTIONS site/<c:[\w-]+>' => '',
                'OPTIONS auth/<c:[\w-]+>' => '',
                'OPTIONS user/<c:[\w-]+>' => '',
            ],
        ],
    ],
    'params' => $params,
];
